package com.xwaf.platform.web.controller.auth;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xwaf.platform.common.api.CommonResponse;
import com.xwaf.platform.common.api.constants.BaseConstants;
import com.xwaf.platform.common.api.entity.ELUser;
import com.xwaf.platform.common.web.BaseController;
import com.xwaf.platform.system.api.entity.DataBase;
import com.xwaf.platform.system.api.entity.auth.UserPermission;
import com.xwaf.platform.system.api.service.auth.UserPermissionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 
 * <p>
 * 用户权限表
 * <p>
 * 
 * @author 李聪 <br>
 * @email xwaf_love@yeah.net <br>
 * @since JDK 1.8<br>
 * @date 2019-5-25 12:21:24 <br>
 * @see 无<br>
 *      Copyright (c) 2019, xwaf_love@yeah.net All Rights Reserved.<br>
 */
@ApiIgnore
@RestController
@Api(tags = "用户权限", hidden = true)
@RequestMapping(value = "/m/userPermission/")
public class UserPermissionController extends BaseController {

	@Autowired
	private UserPermissionService userPermissionService;

	@ApiOperation(value = "分页查询", response = UserPermission.class)
	@PostMapping(value = "page", produces = "application/json;charset=UTF-8")
	public CommonResponse page(@RequestParam("currentPage") String currentPage,
			@RequestParam("pageSize") String pageSize, @RequestBody UserPermission userPermission) {
		CommonResponse resp = CommonResponse.createCommonResponse();
		if (!StringUtils.isNotBlank(currentPage) & !StringUtils.isNotBlank(pageSize)) {
			currentPage = "1";
			pageSize = "10";
		}
		Page<UserPermission> page = new Page<UserPermission>(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
		Page<UserPermission> result = userPermissionService.selPageList(page, userPermission);
		resp.setData(result);
		return resp;
	}

	@ApiOperation(value = "根据条件查询用户权限表", notes = "根据条件查询用户权限表信息", httpMethod = "GET", response = UserPermission.class, produces = MediaType.APPLICATION_JSON_VALUE)
	@GetMapping(value = "userPermissions", produces = "application/json;charset=UTF-8")
	public CommonResponse list(@RequestParam("name") String name) {
		Wrapper<UserPermission> wrapper = new EntityWrapper<UserPermission>();
		wrapper.like("name", name);
		List<UserPermission> list = userPermissionService.selectList(wrapper);
		return CommonResponse.createCommonResponse(list);
	}

	@ApiOperation(value = "新增用户权限表", notes = "新增用户权限表信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	@PostMapping(value = "add", produces = "application/json;charset=UTF-8")
	public CommonResponse addUserPermission(@RequestBody UserPermission userPermission) {
		ELUser loginUser = getLoginUser();
		if (loginUser != null) {
			userPermission.setCreateBy(loginUser.getId());
			userPermission.setUpdateBy(loginUser.getId());
		}
		userPermission.setId(DataBase.getDataBaseId(DataBase.UP));
		boolean isAdded = userPermissionService.insert(userPermission);
		if (isAdded) {
			return CommonResponse.createCommonResponse(userPermission);
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "新增失败!");
		}
	}

	@ApiOperation(value = "修改用户权限表", notes = "修改用户权限表信息", httpMethod = "PUT", produces = MediaType.APPLICATION_JSON_VALUE)
	@PutMapping(value = "update/{userPermissionId}", produces = "application/json;charset=UTF-8")
	public CommonResponse updateUserPermission(@PathVariable("userPermissionId") String userPermissionId,
			@RequestBody UserPermission userPermission) {
		ELUser loginUser = getLoginUser();
		if (loginUser != null) {
			userPermission.setUpdateBy(loginUser.getId());
		}
		userPermission.setId(userPermissionId);
		boolean isUpdated = userPermissionService.updateById(userPermission);
		if (isUpdated) {
			return CommonResponse.createCommonResponse();
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "修改失败!");
		}
	}

	@ApiOperation(value = "删除用户权限表", hidden = true, notes = "删除用户权限表信息", httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
	@DeleteMapping(value = "delete/{userPermissionIds}", produces = "application/json;charset=UTF-8")
	public CommonResponse delById(@PathVariable("userPermissionIds") List<String> userPermissionIds) {
		// boolean isDel =
		// userPermissionService.deleteBatchIds(userPermissionIds);
		boolean isDel = true;// 暂时提供实现
		if (isDel) {
			return CommonResponse.createCommonResponse();
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "删除失败!");
		}
	}

	@ApiOperation(value = "查看用户权限表详情", notes = "查看用户权限表详情信息", httpMethod = "GET", response = UserPermission.class, produces = MediaType.APPLICATION_JSON_VALUE)
	@GetMapping(value = "info/{userPermissionId}", produces = "application/json;charset=UTF-8")
	public CommonResponse getInfoById(@PathVariable("userPermissionId") String userPermissionId) {
		UserPermission userPermission = userPermissionService.selectById(userPermissionId);
		return CommonResponse.createCommonResponse(userPermission);
	}
}
